package com.example.eureka_order.Config;

import com.example.eureka_order.utils.dataSourceUtil.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DynamicDatasourceConfig {
    private static final String wechat = "wechat.datasource";
    private static final String wireless = "wireless.datasource";

    /**
     * 加载application.properties里面的自定义datasource配置信息
     */
    @Bean(name = "wechat_properties")
    @Primary
    @ConfigurationProperties(prefix = wechat)
    public DataSourceProperties dataSourceProperties_wechat() {
        return new DataSourceProperties();
    }

    @Bean(name = "wechat_dataSource")
    @Primary
    @ConfigurationProperties(prefix = wechat)
    public DataSource setDataSource_wechat(@Qualifier("wechat_properties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }

    @Bean(name = "wireless_properties")
//    @Primary
    @ConfigurationProperties(prefix = wireless)
    public DataSourceProperties dataSourceProperties_wireless() {
        return new DataSourceProperties();
    }

    @Bean(name = "wireless_dataSource")
//    @Primary
    @ConfigurationProperties(prefix = wireless)
    public DataSource setDataSource_wireless(@Qualifier("wireless_properties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }

    /**
     * 生成dynamicDatasource
     */
    @Bean(name = "dynamicDatasource")
//    @Primary
    public DataSource dynamicDatasource(@Qualifier("wechat_dataSource") DataSource wechat_dataSource, @Qualifier("wireless_dataSource") DataSource wireless_dataSource) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> paramMap = new HashMap<>();
        paramMap.put("wechatDataSource", wechat_dataSource);
        paramMap.put("wireLessDataSource", wireless_dataSource);
        dynamicDataSource.setTargetDataSources(paramMap);

        dynamicDataSource.setDefaultTargetDataSource(wechat_dataSource);
        return dynamicDataSource;
    }


    /**
     * (事务管理)transaction manager, use JtaTransactionManager for global tx
     */
    @Bean
    public PlatformTransactionManager setTransactionManager(@Qualifier("dynamicDatasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     *
     * */
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("dynamicDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        /**如果有mapper文件在这里设置*/
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/base/*.xml"));
        return bean.getObject();
    }


}
